const util = require('../../utils/util')
Component({
  properties: {
    left: {
      type: Number,
      value: 0
    },
    top: {
      type: Number,
      value: 0
    }
  },
  data: {
    startX: 0,
    startY: 0,
    moveX: 0,
    moveY: 0,
    windowHeight: 0,
    windowWidth: 0,
    buttonWidth: 50,
    buttonHeight: 50
  },
  attached () {
    const { windowWidth, windowHeight } = wx.getSystemInfoSync() // 获取屏幕
    console.log(windowHeight, windowWidth)
    this.setData({
      windowHeight,
      windowWidth
    })
  },
  methods: {
    onTouchStart: function (e) {
      this.setData({
        startX: e.touches[0].pageX,
        startY: e.touches[0].pageY
      })
    },
    onTouchMove: function (e) {
      // 防抖函数，将 this.setData 函数延迟执行 50 毫秒
      const setData = () => {
        this.setData({
          moveX: e.touches[0].pageX - this.data.startX,
          moveY: e.touches[0].pageY - this.data.startY,
          left: this.data.left + e.touches[0].pageX - this.data.startX,
          top: this.data.top + e.touches[0].pageY - this.data.startY,
          startX: e.touches[0].pageX,
          startY: e.touches[0].pageY
        })
      }
    util.debounce(setData, 50)()
    },

    onTouchMoveMethod (e) {
      this.setData({
        moveX: e.touches[0].pageX - this.data.startX,
        moveY: e.touches[0].pageY - this.data.startY,
        left: this.data.left + e.touches[0].pageX - this.data.startX,
        top: this.data.top + e.touches[0].pageY - this.data.startY,
        startX: e.touches[0].pageX,
        startY: e.touches[0].pageY
      })
    },
    
    onTouchEnd: function (e) {
      this.setData({
        startX: 0,
        startY: 0,
        moveX: 0,
        moveY: 0
      })
    }
  }
})
